package com.example.reader.widget;

import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blankj.utilcode.utils.BarUtils;
import com.example.reader.R;
import com.example.reader.ui.base.Mapplication;
import com.example.reader.widget.mysnackbar.GoodSnackbar;

import static android.view.Gravity.CENTER;
import static android.view.Gravity.TOP;

/**
 * Created by Administrator on 2017/3/9.
 */

public class DialogSet {

    /**
     * 顶部出来snackbar
     *
     * @param view
     * @param text
     * @param duration
     * @return
     */
    public static GoodSnackbar getBookShelfSnackbar(FrameLayout view, String text, int duration) {
        GoodSnackbar make = GoodSnackbar.make(view, text, duration);
        int snackbarHeight = make.getHeight();
        int height = BarUtils.getStatusBarHeight(Mapplication.context);
        make.setHeight(snackbarHeight + height);
        make.setMessageGravity(CENTER);
        View view1 = make.getView();
        view1.setPadding(0,height,0,0);
        view1.setBackgroundColor(view.getResources().getColor(R.color.app_notice_bg));
        TextView tv_msg = (TextView) view1.findViewById(R.id.tv_msg);
        tv_msg.setTextSize(15);
        tv_msg.setTextColor(view.getResources().getColor(R.color.white));
        return make;
    }


    /**
     * 顶部出来snackbar
     *
     * @param view
     * @param text
     * @param duration
     * @return
     */
    public static Snackbar showSnackbar(CoordinatorLayout view, CharSequence text, int duration) {
        Snackbar make = getSnackbar(view, text, duration);
        return make;
    }

    @NonNull
    private static Snackbar getSnackbar(ViewGroup view, CharSequence text, int duration) {
        Snackbar make = Snackbar.make(view, text, duration);
        View makeView = make.getView();
        if (view instanceof CoordinatorLayout) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) makeView.getLayoutParams();
            layoutParams.gravity = TOP;
            makeView.setLayoutParams(layoutParams);
        } else if (view instanceof FrameLayout) {
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) makeView.getLayoutParams();
            layoutParams.gravity = TOP;
            makeView.setLayoutParams(layoutParams);
        } else {
            new RuntimeException("view only be CoordinatorLayout or FrameLayout");
        }
        make.show();
        return make;
    }

}
